package com.sport.setting.service.impl;

import com.sport.apis.annotation.DIAccountUserBo;
import com.sport.common.redis.CacheService;
import com.sport.common.redis.RedisKey;
import com.sport.model.common.dtos.ResponseResult;
import com.sport.model.common.enums.AppHttpCodeEnum;
import com.sport.model.setting.dtos.SportEventsGradeSettingDto;
import com.sport.model.setting.dtos.SportPreInfoSettingDto;
import com.sport.model.setting.dtos.SportPreInfoUpdateDto;
import com.sport.model.setting.pojos.SportPreInfo;
import com.sport.model.user.bos.AccountUserBo;
import com.sport.setting.repository.SportPreInfoRepository;
import com.sport.setting.service.SportPreInfoService;
import com.sport.utils.common.BaseUtils;
import com.sport.utils.common.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

@Service
public class SportPreInfoServiceImpl implements SportPreInfoService {

    private SportPreInfoRepository sportPreInfoRepository;

    private CacheService cacheService;

    public SportPreInfoServiceImpl(@Autowired SportPreInfoRepository sportPreInfoRepository,
                                   @Autowired CacheService cacheService) {
        this.sportPreInfoRepository = sportPreInfoRepository;
        this.cacheService = cacheService;
    }

    /**
     * 创建运动会
     * @param sportPreInfoSettingDto
     * @return
     */
    @Override
    @DIAccountUserBo
    public ResponseResult createSportMeeting(SportPreInfoSettingDto sportPreInfoSettingDto) {

        SportPreInfo sportMeeting = sportPreInfoRepository.createSportMeeting(sportPreInfoSettingDto);

        if (sportMeeting == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        ThreadPoolUtils.putOperationToThread(()->{
            cacheService.incrMapFieldValue(RedisKey.USER_INFORMATION + sportPreInfoSettingDto.getAccountUserBo().getSchoolTenementId()
                    , "sportStatus", 1d);
        });

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @DIAccountUserBo
    public ResponseResult updateSportMeeting(SportPreInfoUpdateDto sportPreInfoUpdateDto) {

        SportPreInfo preInfo = sportPreInfoRepository.updateSportMeeting(sportPreInfoUpdateDto);
        if (preInfo == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult obtainSportGrade() {
        List<String> sportGradeList = sportPreInfoRepository.returnSportGradeList(AccountUserBo.getUser().getSchoolTenementId());

        if (sportGradeList == null || sportGradeList.size() == 0)
            return ResponseResult.okResult("当前没有数据记录举行过的运动会！请添加比赛组");

        return ResponseResult.okResult(sportGradeList);
    }

    @Override
    @DIAccountUserBo
    public ResponseResult chooseSportGrade(SportEventsGradeSettingDto sportEventsGradeSettingDto) {

        List<String> newGradeNames = sportEventsGradeSettingDto.getGradeNames();

        List<String> originGradeName = sportEventsGradeSettingDto.getOriginGradeName();

        if (originGradeName == null)
            originGradeName = sportPreInfoRepository.getSportOriginGrade(sportEventsGradeSettingDto.getAccountUserBo().getSchoolTenementId());

        List<String> newElements = BaseUtils.findListsDifferentElement(originGradeName, newGradeNames);

        List<String> deleteElement = BaseUtils.findListsDifferentElement(newGradeNames, originGradeName);

        ThreadPoolUtils.putOperationToThread(()->{
            SportEventsGradeSettingDto copyDtos = BaseUtils.copyProperties(sportEventsGradeSettingDto, SportEventsGradeSettingDto.class);
            copyDtos.setControlTargetGrade(newElements);
            sportPreInfoRepository.insertSportGrade(copyDtos);
        });

        ThreadPoolUtils.putOperationToThread(()->{
            SportEventsGradeSettingDto copyDtos = BaseUtils.copyProperties(sportEventsGradeSettingDto, SportEventsGradeSettingDto.class);
            copyDtos.setControlTargetGrade(deleteElement);
            sportPreInfoRepository.deleteSportGrade(copyDtos);
        });

        return ResponseResult.okResult("请耐心等待！");
    }

    /**
     * 查询运动会前置消息
     * @param schoolTenementId
     * @return
     */
    @Override
    public ResponseResult getPreInfo(String schoolTenementId) {
        SportPreInfo preInfo = sportPreInfoRepository.getPreInfo(schoolTenementId);
        return ResponseResult.okResult(preInfo);
    }

    /**
     * 初始化运动会
     * @param schoolTenementId
     * @return
     */
    @Override
    public ResponseResult initSportMeeting(String schoolTenementId) {
        return sportPreInfoRepository.initSportMeeting(schoolTenementId);
    }
}
